package com.idroc.coelacanth.core.message.support;

import org.jboss.netty.buffer.ChannelBuffer;

import com.idroc.coelacanth.core.message.AbstractMessage;
import com.idroc.coelacanth.core.message.Message;

/**
 * 同步消息
 * @author yuyoo (yuyoo4j@163.com)
 * @teme 2010-6-5 上午05:22:32
 */
public class SyncMessage extends AbstractMessage {
	
	private String id = null;
	
	private boolean isReq = true;
	
	private Message content = null;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public boolean isReq() {
		return isReq;
	}

	public void setReq(boolean isReq) {
		this.isReq = isReq;
	}

	public Message getContent() {
		return content;
	}

	public void setContent(Message content) {
		this.content = content;
	}  

	@Override
	public void encode(ChannelBuffer ob) {

		encodeString(ob, id);
		ob.writeByte((byte) (isReq ? 1 : 0));
		encodeMessage(content, ob); 
	}
	
	@Override
	public void decode(ChannelBuffer ib) {
		
		id = decodeString(ib);
		int flag = ib.readByte();
		isReq = !(0 == flag);
		content = decodeMessage(ib);
	}

	@Override
	public String toString() {

		StringBuilder sb = new StringBuilder();
		sb.append("SyncMessage{");
		sb.append("sn:").append(this.getSN()).append(",");
		sb.append("id:").append(id).append(",");
		sb.append("isReq:").append(isReq).append(",");
		sb.append("content:").append(content);
		sb.append("}");
		return sb.toString();
	}
}
